Image maps are popular, despite the bandwidth they waste and the time they take to load. Users like using them and Web page designers like designing them-perhaps because image maps are one of the few elements of a Web page that truly distinguish the Web from other media.
Why use client-side image maps? Using client-side image maps means you don't have to create a server-side script and program in CGI. All the processing work is done at the browser; to the server, a page with client-side image maps is just another HTML page. Server-side image maps can operate only by sending a special message to the server that is then interpreted by the image map script file. Only then is the correct page dispatched from the server. As a result, tasks such as displaying varying text when a mouse passes over each area of a map are not possible without client-side scripting.
The client-side image map example I use in this chapter would not usually see the light of day on my computer, because the graphic I use is 27KB-huge in comparison to my usual mean, sub-10KB images. Regardless, client-side image maps are fun to work with, and the one I use in this chapter is also an example of two-dimensional HTML using the HTML layout control. The text box that displays a message as the mouse passes over the image actually sits on top of the image itself. More astute designers can take this concept further and make the message appear to be part of the image.
The following example uses an HTML layout to hold the image and handle the mouse events that give the image map its functionality:
Note |
When you develop layouts that include graphics such as truck.gif, you need to specify an absolute path for the graphic. However when you are ready to put the page, the .ALX HTML layout file, and the graphic online, you can edit the PicturePath property from the source of the .ALX file to make it a relative path on the server. |
The next stage is to work out which parts of the image are clickable and act as hyperlinks to other pages in the Web site.
When dealing with image maps in VBScript, you use rectangles to determine the clickable areas. You specify the virtual rectangles with two coordinates, representing the top, left corner and bottom, right corner.
The two coordinates are known as X1,Y1 and X2,Y2. X is the horizontal axis, and Y is the vertical axis; both axes have their origins in the top, left corner of the screen.
Therefore, you need to specify four values for each clickable area: X1, Y1, X2, and Y2. X1 is the number of pixels from the left side of the layout to the left edge of the clickable area. X2 is the number of pixels from the left side of the layout to the right edge of the clickable area. Y1 is the number of pixels from the top edge of the layout to the top edge of the clickable area. And finally, Y2 is the number of pixels from the top edge of the layout to the bottom edge of the clickable area.
To be sure that I specified values that are understood by the layout control, I found it easiest to use the layout control itself to view the values of X and Y. You can do this by adding a very simple line of code to the MouseMove event of the image as follows:
You then create a table of values like the one shown in Table
17.1, which is your map.
You are now ready to add the code that turns your image into an image map.
The main functionality of any image map is the capability to click an area of the graphic and have that mouse click translated into an instruction to navigate to a different URL. However, the ActiveX Image control does not include a Click event. It does have a MouseDown event, and in reality, a Click event is the joining together of two other events-MouseDown and MouseUp. The MouseDown event is fired when the mouse button is pressed, which works just as well as a click for your purposes.
One thing that server-side image maps cannot do is track the mouse as it passes over the image. For this, you need a MouseMove event, which you will program to display relevant messages about the area of the image that the mouse is currently over.
If (InArea(x, y, 50, 29, 154, 92)=true) Then TextBox1.Text = "Our routes, pick-ups, and drop-offs" ElseIf (InArea(x, y, 124, 92, 190, 168)=true) Then TextBox1.Text = "Meet the management team" ElseIf (InArea(x, y, 375, 10, 434, 96)=true) Then TextBox1.Text = "Our Rates.. the best in the business" ElseIf (InArea(x, y, 344, 97, 403, 184)=true) Then TextBox1.Text = "Job vacancies with Web Trucking" Else TextBox1.Text = "Welcome to the Web Trucking Web Site" End If
If (InArea(x, y, 50, 29, 154, 92)=true) Then Window.location.href = "truck2.html" ElseIf (InArea(x, y, 124, 92, 190, 168)=true) Then Window.location.href = "truck3.html" ElseIf (InArea(x, y, 375, 10, 434, 96)=true) Then Window.location.href = "truck1.html" ElseIf (InArea(x, y, 344, 97, 403, 184)=true) Then Window.location.href = "truck4.html" End If
Let's look at the image map in action within the browser. Figure 17.9 shows that, as the mouse is moved around the image, the MouseMove event is fired, causing text to be displayed in the text box.
Figure 17.9 : As you pass the cursor over an area, the text changes.
As you move the cursor to another area of the image map, the text in the text box changes, as shown in Figure 17.10.
Figure 17.10: Move to another area of the image map, and the text changes.
Clicking on an area of the image map causes another page of the Web site to be loaded, as shown in Figure 17.11.
Figure 17.11: What happens when you click an area? You access other pages in the Web site.
Listing 17.1 contains the complete source code for the image map example, truck.alx.
Listing 17.1. The truck.alx code.
<SCRIPT LANGUAGE="VBScript"> <!-- Sub Image1_MouseMove(Button, Shift, X, Y) rem TextBox1.Text = "X=" & X & " Y=" & Y If (InArea(x, y, 50, 29, 154, 92)=true) Then TextBox1.Text = "Our routes, pick-ups, and drop-offs" ElseIf (InArea(x, y, 124, 92, 190, 168)=true) Then TextBox1.Text = "Meet the management team" ElseIf (InArea(x, y, 375, 10, 434, 96)=true) Then TextBox1.Text = "Our Rates.. the best in the business" ElseIf (InArea(x, y, 344, 97, 403, 184)=true) Then TextBox1.Text = "Job vacancies with Web Trucking" Else TextBox1.Text = "Welcome to the Web Trucking Web Site" End If end sub Sub Image1_MouseDown(Button, Shift, X, Y) If (InArea(x, y, 50, 29, 154, 92)=true) Then Window.location.href = "truck2.html" ElseIf (InArea(x, y, 124, 92, 190, 168)=true) Then Window.location.href = "truck3.html" ElseIf (InArea(x, y, 375, 10, 434, 96)=true) Then Window.location.href = "truck1.html" ElseIf (InArea(x, y, 344, 97, 403, 184)=true) Then Window.location.href = "truck4.html" End If end sub Function InArea(x, y, ax1, ay1, ax2, ay2) InArea = x>=ax1 AND x<=ax2 AND y>=ay1 AND y<=ay2 end function --> </SCRIPT> <DIV ID="Layout1" STYLE="LAYOUT:FIXED;WIDTH:450pt;HEIGHT:254pt;"> <OBJECT ID="Image1" CLASSID="CLSID:D4A97620-8E8F-11CF-93CD-00AA00C08FDF" STYLE="TOP:0pt;LEFT:0pt;WIDTH:450pt;HEIGHT:253pt;ZINDEX:0;"> <PARAM NAME="PicturePath" VALUE="f:\llww\chapter17\truck.gif"> <PARAM NAME="BorderStyle" VALUE="0"> <PARAM NAME="SizeMode" VALUE="3"> <PARAM NAME="Size" VALUE="15875;8925"> <PARAM NAME="PictureAlignment" VALUE="0"> <PARAM NAME="VariousPropertyBits" VALUE="19"> </OBJECT> <OBJECT ID="TextBox1" CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3" STYLE="TOP:64pt;LEFT:195pt;WIDTH:146pt;HEIGHT:74pt;TABINDEX:0;ZINDEX:1;"> <PARAM NAME="VariousPropertyBits" VALUE="2894088219"> <PARAM NAME="ForeColor" VALUE="4227327"> <PARAM NAME="Size" VALUE="5151;2611"> <PARAM NAME="Value" VALUE="Welcome to the Web Trucking Inc. Web Site"> <PARAM NAME="FontEffects" VALUE="1073741825"> <PARAM NAME="FontHeight" VALUE="240"> <PARAM NAME="FontCharSet" VALUE="0"> <PARAM NAME="FontPitchAndFamily" VALUE="2"> <PARAM NAME="ParagraphAlign" VALUE="3"> <PARAM NAME="FontWeight" VALUE="700"> </OBJECT> </DIV>
Creating image maps with VBScript and the HTML layout control is very straightforward and enables you to use techniques that are not available elsewhere. The key is to accurately specify the boundaries of each clickable area, ensuring that they do not overlap.
Now look at these other chapters about the HTML layout control and interacting with the browser itself:
Can any graphic be used as an image map? | |
Yes. The format and type of graphic are unimportant. If you can display it through the image control, you can use it as an image map. | |
I tried using my exisiting coordinate table with my image map in VBScript with the layout control as described in this chapter, and it didn't work properly. Why is that? | |
The measurement system of the layout control is different from the normal HTML page that your current coordinates relate to. Therefore, you must re-measure your X and Y coordinates. |